<template>
    <view class="cu-modal" :class="showDialog ? 'show': '' ">
        <view class="cu-dialog text-grey" style="width:85%;">
            <view class="cu-bar bg-white justify-end">
                <view class="content">授权获取您的手机号</view>
                <view class="action" @tap="hide">
                    <text class="cuIcon-close"></text>
                </view>
            </view>
            <view class="padding">
                <view class="text-center"><img :src="$tools.j('logo.png')" style="width:60px;height:50px;"/></view>
                <view class="margin-top">
                    <view class="text-grey">授权获取手机号用户购票短信及时提醒等</view>
                </view>
            </view>
            <view class="cu-bar padding">
                <view class="action margin-0 flex-sub  solid-left" @tap="">
                    <button class="cu-btn lg bg-red radius response" open-type="getPhoneNumber"  @getphonenumber="getphonenumber">手机号授权</button>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
    export default {
        props:{
            wxloginInfo       :{
                type:Object
            }
        },
        data(){
           return  {
               showDialog:false
            }
        },
        methods:{
            show(){
                this.showDialog = true
            },
            hide(){
                this.showDialog = false
            },
            getphonenumber(e){
                console.log('getphonenumber')
                console.log(e);
                this.$user.wx_login_info().then(res=>{
                    if(res){
                        let { encryptedData, iv } = e.detail
                        let { session_key }       = res
                        //拒绝的
                        if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
                            console.log('拒绝授权')
                        } else {
                            console.log('同意授权');
                            this.$api.grantphone({ encryptedData, iv,session_key}).then(res => {
                                console.log('授权结果')
                                console.log(res)

                                this.$store.dispatch('user/bindPhone',res).then(res2=>{
                                    if(res2){
                                        console.log('授权手机号成功')
                                        this.$emit('authorizePhoneOk',res2)
                                        this.hide();
                                    }
                                })
                            })
                        }
                    }

                })
            },
        },
        created() {
        }
    }
</script>

<style scoped>

</style>
